/**
  ******************************************************************************
  * @file    
  * @author  
  * @version 
  * @date    
  * @brief   
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "DRV_Main.h"
#include "SVS_Main.h"
#include "DEV_Main.h"
#include "App_Main.h"
#include "cli-interactor.h"
#include "cli-interpreter.h"
#include "cli.h"
#include "OPP_Debug.h"
#include "fal_port.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
TaskHandle_t INITTask_Handler; 
//初始任务 用于创建其他任务,创建后自动删除
#define INIT_TASK_PRIO 1              /* 任务优先级   */
#define INIT_STK_SIZE  256            /* 任务堆栈大小 */

const char logo[]=\
    "................XXX........XXXX.......XXX..........\r\n"
    "...............X..X.......X...X........X...........\r\n"
    "..............X...........X............X...........\r\n"
    "..............X...........X............X...........\r\n"
    "..............X..XXX......X............X...........\r\n"
    "..............X...X.......X............X...........\r\n"
    "...............X..X.......X...X........X...X.......\r\n"
    "................XX.........XXX........XXXXXX.......\r\n";


void printLogo(void)
{
    printf("%s\r\n",logo);
}



void task_cli(void * pvParameters)
{
    int c;

    cliInit();
    while (1)
    {
        c=fgetc(stdin);
        if(c != EOF){
            CliIttInput(CLI_CHL_COM,(char)c);
        }
        cliLoop();
        vTaskDelay(20 / portTICK_PERIOD_MS);
    }
}

void CLI_LOOP()
{
    int c;

    c=fgetc(stdin);
    if(c != EOF){
        CliIttInput(CLI_CHL_COM,(char)c);
    }
    cliLoop();
}

void MainPrintInfoDelay(int timeout)
{
    static U32 tick = 0;

    if(0 == tick){
        tick = OppTickCountGet();
    }else if(OppTickCountGet() - tick > timeout){
        DEBUG_LOG(DEBUG_MODULE_MAIN, DLL_ERROR, "MainThread run..........\r\n");
        tick = 0;
    }
}


void main_task(void * pvParameters)
{
    while (1)
    {
        DRV_MAIN_LOOP();
        DEV_MAIN_LOOP();
        SVS_MAIN_LOOP();
        APP_MAIN_LOOP();
        CLI_LOOP();
        MainPrintInfoDelay(10000);
        WDG_Feed(*(int *)pvParameters);
        vTaskDelay(20 / portTICK_PERIOD_MS);
    }
}

TASK_PARA_T task_tbl[TASK_ID_MAX]={
    {.id=TASK_ID_MAIN, .task=main_task, .name="main_task", .stacksize=4096, .pri=configMAX_PRIORITIES-2, .enCreate=1, .feedWdg=1},
    {.id=TASK_ID_EC600,.task=Ec600sThread, .name="ec600_task", .stacksize=4120, .pri=configMAX_PRIORITIES-3,  .enCreate=1, .feedWdg=1},
    {.id=TASK_ID_RB,.task=rbTask, .name="rb_task", .stacksize=1024, .pri=configMAX_PRIORITIES-3, .enCreate=1, .feedWdg=1},
};

//初始任务任务函数
void init_task(void *pvParameters)
{
    fdb_err_t err;
    fdb_kvdb_t handle;

    err=fdb_init(&handle);
    if(err!=0)
    {
        printf("fdb_init err:%d!!!\r\n",err);
    }

    SVS_MAIN_INIT();
    DEV_MAIN_INIT();
    APP_MAIN_INIT();
    DEBUG_LOG_INIT();
    cliInit();
    
	taskENTER_CRITICAL();            //进入临界区
    /* 创建任务 */
    xTaskCreate(TaskWDG,                  "wdg_task",        (128), NULL, configMAX_PRIORITIES-1, NULL);
    for(TASK_ID_EN i=TASK_ID_MAIN; i<TASK_ID_MAX; i++){
        if(task_tbl[i].enCreate){
            BaseType_t res = xTaskCreate(task_tbl[i].task,task_tbl[i].name,(task_tbl[i].stacksize/sizeof(portSTACK_TYPE)), &task_tbl[i].id, task_tbl[i].pri, &task_tbl[i].handle);
            if(res != pdPASS){
                printf("create task %s err\r\n",task_tbl[i].name);
            }
            printf("create task %s\r\n",task_tbl[i].name);
            vTaskDelay(10);
        }
    }
    taskEXIT_CRITICAL();           //退出临界区
    vTaskDelete(INITTask_Handler); //删除初始任务
}
 
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4    
    DRV_MAIN_INIT();
    printLogo();
    
    //初始任务
    xTaskCreate((TaskFunction_t )init_task,             //任务函数
                (const char*    )"init_task",           //任务名称
                (uint16_t       )INIT_STK_SIZE,         //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )INIT_TASK_PRIO,        //任务优先级
                (TaskHandle_t*  )&INITTask_Handler);    //任务句柄              
    vTaskStartScheduler();    
}



